package com.huitone.gddw.service.impl.analysis;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.analysis.ISmCallTestDao;
import com.huitone.gddw.dao.entity.SmCallTest;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.analysis.ISmCallTestService;
import com.huitone.gddw.service.dto.analysis.SmCallTestDTO;
import com.huitone.gddw.utils.SysLoginUtil;

@Service
public class SmCallTestService implements ISmCallTestService {
	
	@Autowired
	private ISmCallTestDao smCallTestDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;

	@Override
	public QueryPage<SmCallTest> selectPage(QueryPage<SmCallTest> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("testPhone", page.getParams().get("testPhone"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SmCallTest> result = smCallTestDao.selectPage(map);
		int totalCount = smCallTestDao.selectCount(map);
		
		QueryPage<SmCallTest> queryPage = new QueryPage<>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public void save(SmCallTest smCallTest) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		List<SmCallTest> result = smCallTestDao.selectPage(map);
//		if (result.size()>0) {
//			smCallTestDao.deleteByPrimaryKey(result.get(0).getRecId());
//		}
		smCallTestDao.insert(smCallTest);
		this.updateParamRec("保存拨测短信信息");
	}

	@Override
	public void update(SmCallTest smCallTest) {
		smCallTest.setLastModTime(new Date());
		smCallTest.setLastModStaffId(SysLoginUtil.getLoginUserId());
		smCallTestDao.updateByPrimaryKey(smCallTest);
		
		this.updateParamRec("更新拨测短信信息");
	}

	@Override
	public void delete(String recId) {
		smCallTestDao.deleteByPrimaryKey(recId);
		this.updateParamRec("删除拨测短信信息");
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SM_CALL_TEST, desc);
	}

	@Override
	public SmCallTest selectByPrimaryKey(String recId) {
		return smCallTestDao.selectByPrimaryKey(recId);
	}

	@Override
	public List<SmCallTestDTO> getRecord(Map<String, Object> params) {
		List<SmCallTestDTO> list = smCallTestDao.getRecord(params);
		return list;
	}

}
